<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sub_settlements', function (Blueprint $table) {
            // 主键 - 雪花ID
            $table->unsignedBigInteger('id')->primary()->comment('子结算单ID(雪花ID)');
            
            // 关联字段
            $table->unsignedBigInteger('tenant_id')->comment('租户ID');
            $table->unsignedBigInteger('settlement_id')->comment('结算单ID');
            $table->unsignedBigInteger('freelancer_id')->comment('自由职业者ID');
            
            // 自由职业者信息冗余字段（用于查询和显示）
            $table->string('freelancer_name', 100)->comment('自由职业者姓名');
            $table->string('freelancer_phone', 20)->comment('自由职业者手机号');
            $table->string('freelancer_id_card', 18)->comment('自由职业者身份证号');
            $table->string('freelancer_bank_account', 50)->nullable()->comment('自由职业者银行账号');
            $table->string('freelancer_bank_name', 100)->nullable()->comment('自由职业者开户银行');
            
            // 金额字段
            $table->decimal('pre_settlement_amount', 15, 2)->default(0)->comment('结算前金额');
            $table->decimal('commission', 15, 2)->default(0)->comment('佣金（实际打款金额）');
            $table->decimal('service_fee', 15, 2)->default(0)->comment('服务费（平台扣点）');
            $table->decimal('service_fee_rate', 5, 4)->default(0)->comment('服务费率（如0.05表示5%）');
            
            // 税费信息
            $table->decimal('tax', 15, 2)->default(0)->comment('个税金额');
            $table->decimal('required_balance', 15, 2)->default(0)->comment('所需余额');

            // 状态管理
            $table->enum('status', [
                '待验证',
                '验证中',
                '待算税',
                '待提交',
                '待发放',
                '发放中',
                '已完成',
                '已取消',
                '发放失败',
                '验证失败',
                '已退票',
            ])->default('待验证')->comment('状态');
            
            $table->string('status_description', 500)->nullable()->comment('状态说明');
            
            // 备注信息
            $table->text('payment_remarks')->nullable()->comment('款项备注');
            
            // 电子回单
            $table->json('electronic_receipt')->nullable()->comment('电子回单信息（JSON格式存储文件信息）');
            
            // 时间戳字段
            $table->timestamp('created_at')->nullable()->comment('创建时间');
            $table->timestamp('updated_at')->nullable()->comment('更新时间');
            $table->timestamp('verified_at')->nullable()->comment('验证完成时间');
            $table->timestamp('submitted_at')->nullable()->comment('提交时间');
            $table->timestamp('distribute_started_at')->nullable()->comment('开始发放时间');
            $table->timestamp('distributed_at')->nullable()->comment('发放完成时间');
            $table->timestamp('cancelled_at')->nullable()->comment('取消时间');
            
            // 操作人员
            $table->unsignedBigInteger('created_by')->nullable()->comment('创建人ID');
            $table->unsignedBigInteger('verified_by')->nullable()->comment('验证人ID');
            $table->unsignedBigInteger('submitted_by')->nullable()->comment('提交人ID');
            $table->unsignedBigInteger('distributed_by')->nullable()->comment('发放操作人ID');
            $table->unsignedBigInteger('cancelled_by')->nullable()->comment('取消操作人ID');
            
            // 外键约束
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->foreign('settlement_id')->references('id')->on('settlements')->onDelete('cascade');
            $table->foreign('freelancer_id')->references('id')->on('freelancers')->onDelete('cascade');
            
            // 索引
            $table->index(['tenant_id', 'settlement_id'], 'idx_tenant_settlement');
            $table->index(['tenant_id', 'freelancer_id'], 'idx_tenant_freelancer');
            $table->index(['tenant_id', 'status'], 'idx_tenant_status');
            $table->index(['tenant_id', 'freelancer_name'], 'idx_tenant_freelancer_name');
            $table->index(['tenant_id', 'freelancer_phone'], 'idx_tenant_freelancer_phone');
            $table->index(['tenant_id', 'freelancer_id_card'], 'idx_tenant_freelancer_id_card');
            $table->index(['tenant_id', 'created_at'], 'idx_tenant_created_at');
            
            // 租户级唯一约束
            $table->unique(['tenant_id', 'settlement_id', 'freelancer_id'], 'uk_tenant_settlement_freelancer');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sub_settlements');
    }
};
